﻿using AutoMapper;
using MediatR;
using Medical.Api.Read.Application.Command.RBAC;
using Medical.Api.Read.Dto.RBAC;
using Medical.Domain.RBAC;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Read.Application.Handler.RBAC
{
    public class GetPermissionHandler : IRequestHandler<GetPermissionCommand, ApiResult<List<PermissionDtos>>>
    {
        private readonly IBaseRepository<PermissionModel> _permissionRepository;
        private readonly IMapper mapper;
        private readonly ILogger<GetPermissionHandler> _logger;

        public GetPermissionHandler(IBaseRepository<PermissionModel> permissionRepository, IMapper mapper, ILogger<GetPermissionHandler> logger)
        {
            _permissionRepository = permissionRepository;
            this.mapper = mapper;
            _logger = logger;
        }
        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<PermissionDtos>>> Handle(GetPermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<PermissionDtos>> res = new ApiResult<List<PermissionDtos>>();
            var permissions = _permissionRepository.GetAll().Where(x => x.ParentId == request.ParentId);
            var dtos = mapper.Map<List<PermissionDtos>>(permissions);
            res.Data = dtos;
            res.Code = ApiEnums.Success;
            res.Msg="获取权限成功";
            return Task.FromResult(res);

        }
    }
}
